const { v4: uuidv4 } = require('uuid');
const Task = require('../models/taskModel');

exports.getAllTasks = async (req, res) => {
    try {
        const validPriorities = ['low', 'medium', 'high'];
        const validStatuses = ['not-started', 'in-progress', 'cancelled', 'completed'];

        // 参数验证
        if (req.query.priority && !validPriorities.includes(req.query.priority)) {
            return res.status(400).json({ error: '无效的优先级参数' });
        }
        if (req.query.status && !validStatuses.includes(req.query.status)) {
            return res.status(400).json({ error: '无效的状态参数' });
        }

        const tasks = await Task.getAll({
            q: req.query.q,
            priority: req.query.priority,
            status: req.query.status
        });
        res.json(tasks);
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
};

exports.createTask = async (req, res) => {
    // 验证日期格式
    const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
    if (!dateRegex.test(req.body.due_date)) {
        return res.status(400).json({ error: '日期格式应为YYYY-MM-DD' });
    }

    const taskData = {
        id: uuidv4(),
        ...req.body,
        created_at: new Date().toISOString()
    };

    try {
        await Task.create(taskData);
        res.status(201).json(taskData);
    } catch (err) {
        res.status(400).json({ error: err.message });
    }
};

exports.updateTask = async (req, res) => {
    try {
        const updated = await Task.update(req.params.id, req.body);
        res.json(updated);
    } catch (err) {
        res.status(400).json({ error: err.message });
    }
};

exports.deleteTask = async (req, res) => {
    try {
        await Task.delete(req.params.id);
        res.status(204).send();
    } catch (err) {
        res.status(400).json({ error: err.message });
    }
};

exports.updateStatus = async (req, res) => {
    try {
        const updated = await Task.updateStatus(req.params.id, req.body.status);
        res.json(updated);
    } catch (err) {
        res.status(400).json({ error: err.message });
    }
};

exports.updateFeedback = async (req, res) => {
    try {
        const updated = await Task.updateFeedback(req.params.id, req.body.feedback);
        res.json(updated);
    } catch (err) {
        res.status(400).json({ error: err.message });
    }
};

exports.updatePriority = async (req, res) => {
    try {
        const { priority } = req.body;
        if (!['low', 'medium', 'high'].includes(priority)) {
            return res.status(400).json({ error: '无效的优先级' });
        }

        const updated = await Task.updatePriority(req.params.id, priority);
        if (updated === 0) {
            return res.status(404).json({ error: '任务不存在' });
        }
        res.json({ success: true });
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
};

exports.getTaskById = async (req, res) => {
    try {
        const task = await Task.getById(req.params.id);
        if (!task) {
            return res.status(404).json({ error: 'Task not found' });
        }
        res.json(task);
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
};